import {defineComponent, ref, onMounted, computed, h, onUnmounted} from 'vue';

import { UserInfo } from '~/src/components/UserInfo';
import Style1080 from './1080.module.scss';
import Stylevedio from './vediotop.module.scss';
import { KEEP_VIDEO_TOP } from '~/src/config';
import {dyhyd, gethyd, gh, ghyjs, jyjs, mzjfPay, mzjfyjs, quhao, qxjs, yjs,} from "@/api/his";

import {ElButton, ElMessage, ElMessageBox} from "element-plus";
import router from "@/router";
import {tempStore} from "@/store/modules/temp";
import {showMessage} from "@/utils/hooks";
import {closeLoading, showLoading} from "@/utils";
import {useBreadcrumbStore} from "@/store/modules/breadcrumb";
import {ghStore} from "@/store/modules/yvyue";

export default defineComponent({
    setup() {
        const Style = KEEP_VIDEO_TOP ? Stylevedio : Style1080;
        const tempstore = tempStore();
        const zfType = ref()
        const isProcessing = ref(false); // 防止重复点击/重复请求
        const breadcrumbStore = useBreadcrumbStore(); // 实例化Pinia
        const lx = breadcrumbStore.titlelx; // 选择的类型
        // 获取挂号选择信息
        const ghstore =ghStore();//实例化pinia存储
        onMounted(() => {});

        // 结算
        const js =async (type:string) => {
            showLoading()
            // 防止重复点击
            if (isProcessing.value) {
                showMessage('正在处理中，请稍候...', 'warning')
                return;
            }
            zfType.value =type;
            isProcessing.value = true;
            switch (lx){
                case '挂号': await ghlc();break; // 挂号结算流程
                case '取号': await qhlc();break; // 取号结算流程
                case '门诊缴费': await mzjflc();break; // 门诊缴费结算流程
            }
            // 重置处理状态
            isProcessing.value = false;
            closeLoading() ;
        }
        // 结算校验
        const jsjyfun = async()=>{
            let resjy = await jyjs({
                orderNo:tempstore.yjsItemInfo?.orderNo
            })
            tempstore.setjyjsItemInfo({
                zje:resjy.rows.zje,
                ybje:resjy.rows.ybje,
                zfje:resjy.rows.zfje,
                lsh:resjy.rows.lsh,
                orderNo:resjy.rows.orderNo,
                paystatus:resjy.rows.status,
            })
        }
        // 挂号结算流程
        const ghlc = async()=>{
            // 1.预结算
            let res =await ghyjs({
                hy:ghstore.hy, doctor: ghstore.doctor,
                xzpayType:zfType.value, // 0医保 1 自费
            })
            tempstore.setyjsItemInfo({
                zfje:res.rows.zfje,
                ybje:res.rows.ybje,
                zje:res.rows.zje,
                lsh:res.rows.lsh,
                orderNo:res.rows.orderNo,
                goType:res.rows.type,//0进入确认结算，1进入扫码待支付
            })
            // 一、全医保结算 0进入结算校验，1进入扫码待支付
            if (res.rows?.gotype === '0'){
                // 2.结算校验
                let resjy = await jyjs({
                    orderNo:tempstore.yjsItemInfo?.orderNo
                })
                tempstore.setjyjsItemInfo({
                    zje:resjy.rows.zje,
                    ybje:resjy.rows.ybje,
                    zfje:resjy.rows.zfje,
                    lsh:resjy.rows.lsh,
                    orderNo:resjy.rows.orderNo,
                    paystatus:resjy.rows.status,
                })
                // 3. 确认挂号
                const res = await gh({hy:ghstore.hy, doctor: ghstore.doctor,ghtype:'0'})
                router.push({
                    path: "/lqpt",
                    query:{}
                })
                // 二、自费+医保结算 / 全自费结算
            }else if (res.rows?.gotype === '1'){
                router.push({
                    path: "/smt",
                    query:{}
                })
            }
        }
        // 取号结算流程
        const qhlc = async()=>{
            // 1.预结算
            let res =await yjs({
                id:tempstore.qh?.id,
                xzpayType:zfType.value, // 0医保 1 自费
            })
            tempstore.setyjsItemInfo({
                zfje:res.rows.zfje,
                ybje:res.rows.ybje,
                zje:res.rows.zje,
                lsh:res.rows.lsh,
                orderNo:res.rows.orderNo,
                goType:res.rows.type,
            })
            // 一、全医保结算
            if (res.rows?.gotype === '0'){
                // 2.结算校验
                let resjy = await jyjs({
                    orderNo:tempstore.yjsItemInfo?.orderNo
                })
                tempstore.setjyjsItemInfo({
                    zje:resjy.rows.zje,
                    ybje:resjy.rows.ybje,
                    zfje:resjy.rows.zfje,
                    lsh:resjy.rows.lsh,
                    orderNo:resjy.rows.orderNo,
                    paystatus:resjy.rows.status,
                })
                // 3.确认取号
                let res = await quhao({
                    id:tempstore.qh,
                    type:'true',
                    orderNo:resjy.rows.orderNo,
                });
                router.push({
                    path: "/lqpt",
                    query:{}
                })
                // 二、自费+医保结算 / 全自费结算
            }else if (res.rows?.gotype === '1'){
                router.push({
                    path: "/smt",
                    query:{}
                })
            }
        }
        // 挂号结算流程
        const mzjflc = async()=>{
            // 1.预结算
            let res = await mzjfyjs({
                xzpayType:zfType.value, // 0医保 1 自费
                ids:JSON.stringify(tempstore.mzjfids),
            })
            tempstore.setyjsItemInfo({
                zfje:res.rows.zfje,
                ybje:res.rows.ybje,
                zje:res.rows.zje,
                lsh:res.rows.lsh,
                orderNo:res.rows.orderNo,
                goType:res.rows.type,
            })
            // 一、全医保结算 0进入结算校验，1进入扫码待支付
            if (res.rows?.gotype === '0'){
                // 2.结算校验
                await jsjyfun();
                // 3. 门诊缴费
                const res = await mzjfPay({ids: JSON.stringify(tempstore.mzjfids)})
                router.push({path:'jfcg'});
                // 二、自费+医保结算 / 全自费结算
            }else if (res.rows?.gotype === '1'){
                router.push({
                    path: "/smt",
                    query:{}
                })
            }
        }
        return () => (
            <>
                <div class={Style.top}>
                    <UserInfo time={10000} ></UserInfo>
                    <div class={Style.container_mar} >
                        <div class={Style.body}>
                            <div class={Style.title}>请选择支付方式</div>
                            <div class={Style.btns}>
                                <ElButton class={Style.btnStyle_item} type="success"  onClick={() => js('0')}>医保结算</ElButton>
                                <ElButton class={Style.btnStyle_item} type="primary" onClick={() => js('1')}>自费结算</ElButton>
                            </div>
                        </div>
                    </div>
                </div>

            </>
        );
    }
})
